# -*- coding: utf-8 -*-
# @Time    : 2024/1/4 21:01
# @Author  : micah
# @File    : 10.使用requests处理cookie.py
# @Software: PyCharm


"""
requests处理cookie的三种方式
    1.可以在headers中声明cookie信息
    2.可以自定义cookie字典之后将字典带入到request对象中
    3.可以使用session方法将cookie传递到第二次请求中
"""

import requests

# 在headers中声明cookie
headers = {
    "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 "
                  "(KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1",

    "Cookie": 'BIDUPSID=07953C6101318E05197E77AFF3A49007; PSTM=1695298085; '
              'ZFY=jAXoBNlaBGlHggda:BLlW8x7pEMyEhiZUIRbuQnnavss:C; '
              'APPGUIDE_10_6_5=1; REALTIME_TRANS_SWITCH=1; '
              'FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; '
              'SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; '
              'BAIDUID=37927E8274D89B902DEB6F1A024B3860:FG=1; '
              'BAIDUID_BFESS=37927E8274D89B902DEB6F1A024B3860:FG=1; '
              'RT="z=1&dm=baidu.com&si=ba30f04e-d552-4a5a-864f-1b2b222ff176&ss=lne882ji&sl=2&tt=1'
              'ju&bcn=https%3A%2F%2Ffclog.baidu.com%2Flog%2Fweirwood%3Ftype%3Dperf&ld=3pn&nu='
              '1dzl78ujc&cl=3bd&ul=61c&hd=622"; BA_HECTOR=a12g8ka22421al2k80ak21a31ihvc081o; '
              'BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; '
              'Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1695476554,1696577209; '
              'Hm_lvt_afd111fa62852d1f37001d1f980b6800=1695476565,1696577271; '
              'Hm_lpvt_afd111fa62852d1f37001d1f980b6800=1696577271; Hm_lpvt_64e'
              'cd82404c51e03dc91cb9e8c025574=1696577271; ab_sr=1.0.1_MjZiYjAyZTQ4OTZkNWU0Y2M'
              '5YjQxMzZiOTE4Y2ZkOWNmMmI2MTNiMzhlOWQ0MTE4MzU0NDg5Njc5ZWU1ZDVkN2E4ZmM2Zjg3NjA5N2IwYWQ3OG'
              'I3ZDBlYWJlMmFmODM3Y2FhZmJkYzgxY2EzZmI1NWRiZDgxNWMxOTU3ZjNhZTk3NzE0ZDg1OGY1MGM4YTM2ZjA1'
              'ZTY4MGViOTI2OTlhYQ=='
}


# 定义cookie字典并将这个字典带入到requests请求中
cookies = {
    'Cookie': 'BAIDUID_BFESS=2CC86375B26DE6C5F9E6D5F228682F01:FG=1; BIDUPSID=2CC86375B26DE6C5F9E6D5F228682F01; PSTM=1704281079; H_PS_PSSID=39938_39999_40024_40044; BD_UPN=123253; ZFY=4WZsRNNBqIPj2GNv2rrXg:BCIOAsJZtTSXEN5nBX4eJo:C; H_WISE_SIDS=39938_39999_40024_40044; COOKIE_SESSION=30_0_0_0_4_0_0_0_0_0_0_0_87_0_64_0_1704281188_0_1704281124%7C2%230_0_1704281124%7C1; BA_HECTOR=ag8k0426ak85208504212k2go0ruob1ipdbgk1t; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598'
}

response = requests.get('http://www.baidu.com', cookies=cookies)
print(response.request.headers)

